<?php
header('content-type:text/html;charset=utf-8');
session_start();
require 'config.php';
error_reporting(5);
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(0);


//身份验证
session_start();
if (!isset($_SESSION['login'])) {
	header('Refresh:2;url=login/index.php');
	echo "你还没有登陆！";
	die();
}




// 获取登录用户id
$email=$_SESSION['login'];
$query_email="SELECT * FROM carts WHERE `uemail`='$email'";
$result_email=mysqli_query($config,$query_email);
$row_email=mysqli_fetch_array($result_email);
// echo $row_email['uemail'];
$user_email=$row_email['buyerid'];





$field=$user_email;
	// $fieldbig=$_GET['actbig'];
	       if($field!=""){
	       	$sql="SELECT * FROM carts WHERE `buyerid`='$field' ";
	       	$result = mysqli_query($config,$sql) or die ('SQL语句有误：'.mysqli_error());
	       }





$tj_name=$_POST['tj_name'];
$tj_tel=$_POST['tj_tel'];
$tj_address=$_POST['tj_address'];
$tj_yzbm=$_POST['tj_yzbm'];




// // 买家信息

$bemail=$_SESSION['login'];
$query_bemail="SELECT * FROM users WHERE `uemail`='$bemail'";
$result_bemail=mysqli_query($config,$query_bemail);
$row_bemail=mysqli_fetch_array($result_bemail);
// echo $row_email['uemail'];
$buyer_email=$row_bemail['userid'];

// 卖家信息
// $conSaler = "SELECT * FROM users WHERE userid='".$row_email['salerid']."'";
// $queSaler = mysqli_query($config,$conSaler);
// $row_semail=mysqli_fetch_array($queSaler);
// var_dump($row_bemail);


$pub_time=Date('Y-m-d H:i:s');
include_once('dbtools.inc.php');
$link=create_connection();




// 买家不能购买自己卖的商品
if($row_bemail['userid']==$reSaler['userid']){
	echo ('<script type="text/javascript">alert("不能购买自己的商品！");
		window.location.href = "cart.php";
		</script>');exit();
}



// $kucun=0;


// var_dump(mysqli_fetch_array($result));
// 循环查询到的数据库内容	
while ($ro=mysqli_fetch_array($result)){



// 未添加则插入数据表



// $query="INSERT INTO order (`tj_id`,`buyerid`,`salerid`,`tj_name`,`tj_tel`,`tj_address`,`tj_yzbm`,`goodsid`,`goodsname`,`cost`,`bigtypeid`,`smalltypeid`,`picture`,`pub_time`)
// VALUES (null,'".$ro['buyerid']."','".$ro['salerid']."','$tj_name','$tj_tel','$tj_address','$tj_yzbm','".$ro['goodsid']."','".$ro['goodsname']."','".$ro['cost']."','".$ro['bigtypeid']."','".$ro['smalltypeid']."','".$ro['picture']."','$pub_time')";



// -----------------------------------------------------------------------
// 插入数据1
$query="INSERT INTO order_tj (`tj_id`,`buyerid`,`salerid`,`saler_name`,`saler_tel`,`saler_address`,`email`,`u_name`,`tj_name`,`tj_tel`,`tj_address`,`tj_yzbm`,`goodsid`,`goodsname`,`cost`,`bigtypeid`,`smalltypeid`,`picture`,`pub_time`,`orderid`,`bianhao`)
VALUES (null,'".$ro['buyerid']."','".$ro['salerid']."','".$ro['saler_name']."','".$ro['saler_tel']."','".$ro['saler_address']."','".$ro['uemail']."','".$ro['buyername']."','$tj_name','$tj_tel','$tj_address','$tj_yzbm','".$ro['goodsid']."','".$ro['goodsname']."','".$ro['cost']."','".$ro['bigtypeid']."','".$ro['smalltypeid']."','".$ro['picture']."','$pub_time','".$ro['orderid']."','".$ro['bianhao']."')";

// -----------------------------------------------------------------------
// 插入数据（备份代码)
// $query="INSERT INTO dindantijiao (`tj_id`,`buyerid`,`salerid`,`email`,`u_name`,`tj_name`,`tj_tel`,`tj_address`,`tj_yzbm`,`pub_time`)
// VALUES (null,'".$ro['buyerid']."','".$ro['salerid']."','".$ro['uemail']."','".$ro['buyername']."','$tj_name','$tj_tel','$tj_address','$tj_yzbm','$pub_time')";

// var_dump($query);




// var_dump($sql);
	       	$sql="SELECT * FROM carts WHERE `uemail`='".$_SESSION['login']."' ";
	       	$result1 = mysqli_query($config,$sql) or die ('SQL语句有误：'.mysqli_error());      
	       // }
	
$kucun=0;
 while ($ro=mysqli_fetch_array($result1)){
// var_dump($ro);
// $id[]=$ro['goodsid'];
$sql2="update goods set kucun='{$kucun}' where goodsid={$ro['goodsid']}";
$res1=execute_sql($link,'school',$sql2);
}


$res=execute_sql($link,'school',$query);



// -----------------------------------------
// session_start();
// 引入数据库
// include_once('dbtools.inc.php');
// include_once('config.php');
// $link=create_connection();


// $field=$_SESSION['login'];
	// $fieldbig=$_GET['actbig'];

	       // if($field!=""){

// var_dump($ro['goodsid']);
// var_dump($sql);





}	

header('Refresh:1;url=order_ok.php');
echo '信息保存成功！';

// echo ('<script type="text/javascript">alert("保存成功！");
//      			window.history.back(-1);
//      			</script>');


	die();
	?>
